package com.baomidou.ant.service.impl;

import com.baomidou.ant.entity.InStorage;
import com.baomidou.ant.entity.SysUser;
import com.baomidou.ant.entity.WorkOrder;
import com.baomidou.ant.mapper.InStorageMapper;
import com.baomidou.ant.service.IInStorageService;
import com.baomidou.ant.service.IPinLibraryService;
import com.baomidou.ant.service.ISysDeptService;
import com.baomidou.ant.service.ISysUserService;
import com.baomidou.ant.vo.request.RequestListInStorageVo;
import com.baomidou.ant.vo.response.ResponseListInStorageVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Objects;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author jobob
 * @since 2022-03-18
 */
@Service
public class InStorageServiceImpl extends ServiceImpl<InStorageMapper, InStorage> implements IInStorageService {
    @Autowired
    ISysUserService userService;
    @Autowired
    ISysDeptService sysDeptService;
    @Autowired
    IPinLibraryService pinLibraryService;
    @Override
    public IPage<ResponseListInStorageVo> listInStorage(RequestListInStorageVo vo) {
        IPage<InStorage> iPage=new Page<>(vo.getDisplayStart(), vo.getDisplayLength());
        QueryWrapper<InStorage> queryWrapper=new QueryWrapper<>();
        //工单号查询
        if(StringUtils.isNotBlank(vo.getInNum())){
            queryWrapper.like("in_num",vo.getInNum());
        }
        //产品类型查询
        if(ObjectUtils.isNotEmpty(vo.getPinId())){
            queryWrapper.eq("pin_id",vo.getPinId());
        }
        SysUser user=userService.getById(vo.getUserId());
        //判断管理员级别
        if(Objects.equals(user.getType(), "1")){
            //管理员条件查询
            if(ObjectUtils.isNotEmpty(vo.getDeptId())){
                queryWrapper.eq("dept_id",vo.getDeptId());
            }
        } else if(Objects.equals(user.getType(), "0")||Objects.equals(user.getType(), "2")){//只能看自己部门的
            queryWrapper.eq("dept_id",user.getDeptId());
        }
        IPage<InStorage> inStorageIPage=page(iPage,queryWrapper);
        return inStorageIPage.convert(inStorage -> {
            ResponseListInStorageVo responseListInStorageVo=new ResponseListInStorageVo();
            BeanUtils.copyProperties(inStorage,responseListInStorageVo);
            responseListInStorageVo.setDeptName(sysDeptService.getById(inStorage.getDeptId()).getDept());
            responseListInStorageVo.setPinName(pinLibraryService.getById(inStorage.getPinId()).getPinName());
            return responseListInStorageVo;
        });
    }
}
